package com.jiezuo.flowable.controller;

import com.jiezuo.auth.entity.User;
import com.jiezuo.common.enums.ResultEnums;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.util.VerifyUtil;
import com.jiezuo.common.vo.Result;
import com.jiezuo.flowable.service.CommonService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *@Description
 *@Author 庞留杰
 *@Date2023/8/4 9:04
 *@Version V1.0
 **/
@Slf4j
@RestController
@Transactional
@RequestMapping("/workflow")
public class CommonController {

    @Autowired
    CommonService commonService;

    @PostMapping("/start")
    public Result<String> start(@RequestBody Map<String, Object> params) {
        String start = commonService.start(params);
        return new ResultUtil<String>().setData(start);
    }

    @PostMapping("/execute")
    public Result<String> execute(@RequestBody Map<String, Object> params) {
        commonService.execute(params);
        return new ResultUtil<String>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    @PostMapping("/apply")
    public Result<List<String>> apply(@RequestParam String processDefinitionKey) {
        List<String> ids = commonService.apply(processDefinitionKey);
        return new ResultUtil<List<String>>().setData(ids);
    }

    @PostMapping("/todo")
    public Result<List<Map<String, Object>>> todo(@RequestParam String processDefinitionKey) {
        List<Map<String, Object>> ids = commonService.todo(processDefinitionKey);
        return new ResultUtil<List<Map<String, Object>>>().setData(ids);
    }

    @PostMapping("/done")
    public Result<List<String>> done(@RequestParam String processDefinitionKey) {
        List<String> ids = commonService.done(processDefinitionKey);
        return new ResultUtil<List<String>>().setData(ids);
    }

    @PostMapping("/withdraw")
    public Result<String> withdraw(@RequestBody Map<String, Object> params) {
        commonService.withdraw(params);
        return new ResultUtil<String>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    //通过流程实例id获取taskId", notes = "通过流程实例id获取taskId")
    @GetMapping("/getTaskIdByProcessInstanceId")
    public Result getTaskIdByProcessInstanceId(@RequestParam(value = "processInstanceId") String processInstanceId) {
        String taskId = commonService.getTaskIdByProcessInstanceId(processInstanceId);
        return new ResultUtil<>().setData(taskId);
    }

    //通过processInstanceId查询待办人员
    @Deprecated //废弃
    @GetMapping("/findTodoPersonsByProcessInstanceId")
    public Result<List<User>> findTodoPersonsByProcessInstanceId(@RequestParam String processInstanceId) {
        List<User> users = commonService.findTodoPersonsByProcessInstanceId(processInstanceId);
        return new ResultUtil<List<User>>().setData(users);
    }

    //删除流程
    @PostMapping("/deleteProcessInstanceId")
    public Result deleteProcessInstanceId(@RequestParam String processInstanceId) {
        commonService.deleteProcessInstanceId(processInstanceId);
        return new ResultUtil().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    @GetMapping("/findAllUser")
    public Result<List<Map<String, String>>> findAllUser() {
        List<Map<String, String>> list = commonService.findAllUser();
        return new ResultUtil<List<Map<String, String>>>().setData(list);
    }

    @GetMapping("/findAllDepart")
    public Result<List<Map<String, String>>> findAllDepart() {
        List<Map<String, String>> list = commonService.findAllDepart();
        return new ResultUtil<List<Map<String, String>>>().setData(list);
    }

    @GetMapping("/findAllPosition")
    public Result<List<Map<String, String>>> findAllPosition() {
        List<Map<String, String>> list = commonService.findAllPosition();
        return new ResultUtil<List<Map<String, String>>>().setData(list);
    }

    @GetMapping("/findAllPositionGroup")
    public Result<List<Map<String, String>>> findAllPositionGroup() {
        List<Map<String, String>> list = commonService.findAllPositionGroup();
        return new ResultUtil<List<Map<String, String>>>().setData(list);
    }

    @GetMapping("/findAllPositionLevel")
    public Result<List<Map<String, String>>> findAllPositionLevel() {
        List<Map<String, String>> list = commonService.findAllPositionLevel();
        return new ResultUtil<List<Map<String, String>>>().setData(list);
    }

    // 保存为草稿

    @PostMapping("/draftForm")
    public Result draftForm(@RequestBody Map<String, Object> params) {
        commonService.draftForm(params);
        return new ResultUtil<>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    // 暂存，用于起草后的数据保存，保存更新业务以及表单数据，改任何业务状态

    @PostMapping("/temporarySaveForm")
    public Result temporarySaveForm(@RequestBody Map<String, Object> params) {
        commonService.temporarySaveForm(params);
        return new ResultUtil<>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    // 用户发起流程时(携带表单信息)

    @PostMapping("/startForm")
    public Result<String> startForm(@RequestBody Map<String, Object> params) {
        String start = commonService.startForm(params);
        return new ResultUtil<String>().setData(start);
    }

    // 用户发起流程时(携带表单信息)

    @PostMapping("/executeForm")
    public Result<String> executeForm(@RequestBody Map<String, Object> params) {
        commonService.executeForm(params);
        return new ResultUtil<String>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    @PostMapping("/apply2")
    public Result<List<Map<String, Object>>> apply2(@RequestParam String processDefinitionKey) {
        List<Map<String, Object>> map = commonService.apply2(processDefinitionKey);
        return new ResultUtil<List<Map<String, Object>>>().setData(map);
    }

    @PostMapping("/todo2")
    public Result<List<Map<String, Object>>> todo2(@RequestParam String processDefinitionKey) {
        List<Map<String, Object>> map = commonService.todo2(processDefinitionKey);
        return new ResultUtil<List<Map<String, Object>>>().setData(map);
    }

    @PostMapping("/done2")
    public Result<List<Map<String, Object>>> done2(@RequestParam String processDefinitionKey) {
        List<Map<String, Object>> map = commonService.done2(processDefinitionKey);
        return new ResultUtil<List<Map<String, Object>>>().setData(map);
    }

    //通过processInstanceId查询待办信息
    @GetMapping("/findTodoInfoByProcessInstanceId")
    public Result<List<Map<String, String>>> findTodoInfoByProcessInstanceId(@RequestParam(required = false) String processInstanceId) {
        VerifyUtil.notEmpty("流程实例ID", processInstanceId);
        List<Map<String, String>> info = commonService.findTodoInfoByProcessInstanceId(processInstanceId);
        return new ResultUtil<List<Map<String, String>>>().setData(info);
    }

    @GetMapping("/message")
    public Result<Map<String, Object>> message() {
        Map<String, Object> result = commonService.message();
        return new ResultUtil<Map<String, Object>>().setData(result);
    }

    //驳回

    @PostMapping("/reject")
    public Result reject(@RequestBody Map<String,Object> map) {
        commonService.reject(map);
        return new ResultUtil<String>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    // 跨节点驳回

    @PostMapping("/crossNodeRejection")
    public Result crossNodeRejection(@RequestBody Map<String,Object> map) {
        commonService.crossNodeRejection(map);
        return new ResultUtil<String>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }

    //删除", notes = "删除【逻辑删除】")
    @PostMapping("/deleteFormAndFlow")
    public Result deleteFormAndFlow(@RequestBody Map<String,Object> params) {
        commonService.deleteFormAndFlow(params);
        return new ResultUtil<String>().setSuccessMsg(ResultEnums.OK.getReasonPhraseCN());
    }


}
